package com.example.adminserivce.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.adminserivce.Mapper.CourseMapper;
import com.example.adminserivce.Mapper.CourseTeacherMapper;
import com.example.adminserivce.Mapper.TeacherMapper;
import com.example.adminserivce.Service.CourseService;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.Course;
import com.example.common.domain.pojo.CourseTeacher;
import com.example.common.domain.pojo.Teacher;
import com.example.common.utils.CacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> getListCourse() {
        // 使用缓存穿透策略：查询存在性校验 + 空值缓存
        return cacheClient.queryWithPassThroughList(
                RdsConstants.RESOURCE_INFO,  // Redis 键前缀
                RdsConstants.ID_ALL_DATE,                          // 主键ID
                Course.class,                 // 实体类类型
                this::getAllCourses,                 // MyBatis-Plus 查询方法
                RdsConstants.CACHE_Long_TTL,        // 长时间数据缓存时间（分钟）
                TimeUnit.MINUTES,
                RdsConstants.CACHE_NULL_TTL,   // 空值缓存时间（分钟）
                TimeUnit.MINUTES
        );
    }

    @Override
    public List<Teacher> getToTeacher(String courseId) {
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(
                Wrappers.<CourseTeacher>lambdaQuery().eq(CourseTeacher::getCourseId, courseId)
        );
        if (courseTeachers.isEmpty()) {
            return Collections.emptyList();
        }
        List<String> teacherIds = courseTeachers.stream()
                .map(CourseTeacher::getTeacherId)
                .toList();
        return teacherMapper.selectList(Wrappers.<Teacher>lambdaQuery().in(Teacher::getId, teacherIds));
    }

    @Override
    public List<Course> getTeacherIdToCourseInfo(String teacherId) {
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(
                Wrappers.<CourseTeacher>lambdaQuery().eq(CourseTeacher::getTeacherId, teacherId)
        );
        if (courseTeachers.isEmpty()) {
            return Collections.emptyList();
        }
        List<String> CourseIds = courseTeachers.stream()
                .map(CourseTeacher::getCourseId)
                .toList();
        return courseMapper.selectList(Wrappers.<Course>lambdaQuery().in(Course::getId, CourseIds));
    }

    public List<Course> getAllCourses(String id) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        return list(queryWrapper);
    }


}
